using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WPCoco.Tilesets
{
    /// <summary>
    /// Collection of TextureFrames within a texture
    /// </summary>
    public partial class Tileset : ICollection<TextureFrame>
    {
        readonly Dictionary<string, int> aliases = new Dictionary<string, int>();

        public void AddAlias(string alias, int index) 
        {
            aliases[alias] = index;
        }

        public int GetFrameIndex(string alias) 
        {
            return aliases[alias];
        }

        readonly List<TextureFrame> frames = new List<TextureFrame>();

        public TextureFrame this[int index]
        {
            get { return frames[index]; }
        }

        public TextureFrame this[string alias]
        {
            get 
            {
                if (!aliases.ContainsKey(alias)) return default(TextureFrame);
                return frames[aliases[alias]];
            }
        }

        public IEnumerable<string> Keys 
        {
            get
            {
                return aliases.Keys;
            }
        }

        public bool Contains(string name) 
        {
            return aliases.Keys.Contains(name);
        }

        public Texture2D Texture { get { return this[0].Texture; } }

        #region ICollection<TextureFrame> Members

        public void Add(TextureFrame item)
        {
            frames.Add(item);
        }

        public void Clear()
        {
            frames.Clear();
        }

        public bool Contains(TextureFrame item)
        {
            return frames.Contains(item);
        }

        public void CopyTo(TextureFrame[] array, int arrayIndex)
        {
            frames.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return frames.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(TextureFrame item)
        {
            return frames.Remove(item);
        }

        #endregion

        #region IEnumerable<TextureFrame> Members

        public IEnumerator<TextureFrame> GetEnumerator()
        {
            return frames.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}
